//
//  HMVisitorLoginView.swift
//  WeiBoSwift
//
//  Created by hgj on 2016/11/16.
//  Copyright © 2016年 HuGuoJian. All rights reserved.
//  访问视图

import UIKit
import SnapKit

// 加 @objc 就可以 加 optional 或者 required    不加 @objc 就是 (默认)
@objc protocol HMVisitorLoginViewDelegate {
    
    @objc optional func userWillLogin()
    
    func userWillRegister()
}

class HMVisitorLoginView: UIView {
    
    // MARK: -----提供给外界的设置界面信息的方法-----
    func updateInfo(title: String, imageName: String, isHome: Bool = false) {
        
        tipLabel.text = title
        circleView.image = UIImage(named: imageName)
        
        if isHome {
            
            animate()
        } else {
            
            backView.isHidden = true
            homeHouse.isHidden = true
        }
    }
    
    private func animate() {
        
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        
        animator.duration = 20.0
        animator.repeatCount = MAXFLOAT
        animator.toValue = M_PI * 2
        
        //当动画效果啊完成之后就从图层移除,设置为false 就不会被移除图层, 默认值值是true
        animator.isRemovedOnCompletion = false
        
        circleView.layer.add(animator, forKey: nil)
    }
    
    lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    lazy var backView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    lazy var homeHouse: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    lazy var tipLabel: UILabel = {
        let label = UILabel(title: "关注一些人,关注一些人,回到这里看看有什么惊喜", titleColor: UIColor.darkGray, fontSize: 14)
        
        label.numberOfLines = 0
        label.textAlignment = .center
        label.preferredMaxLayoutWidth = 220 // 推荐这种方法设置宽度
        
        return label
    }()
    
    lazy var loginButton: UIButton = {
        let button = UIButton(title: "登录", titleColor: UIColor.darkGray, fontSize: 14)
        
        button.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        return button
    }()
    
    lazy var registerButton: UIButton = {
        let button = UIButton(title: "注册", titleColor: UIColor.darkGray, fontSize: 14)
        
        button.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        return button
    }()
    
    // 代理属性
    weak var delegate: HMVisitorLoginViewDelegate?
    
    // MARK: -----自定义构造方法-----
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        
        loginButton.addTarget(self, action: #selector(clickLoginButton(loginBtn:)), for: .touchUpInside)
        
        registerButton.addTarget(self, action: #selector(clickRegisterButton(registerBtn:)), for: .touchUpInside)
        
        addSubview(circleView)
        
        addSubview(backView)
        
        addSubview(homeHouse)
        
        addSubview(tipLabel)
        
        addSubview(loginButton)
        
        addSubview(registerButton)
        
        // 背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        homeHouse.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(circleView)
            make.top.equalTo(circleView.snp.bottom).offset(20)
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.leading.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
    }
    
    @objc private func clickLoginButton(loginBtn: UIButton) {
        delegate?.userWillLogin?()
    }
    
    @objc private func clickRegisterButton(registerBtn: UIButton) {
        delegate?.userWillRegister()
    }
}
